home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 900 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.3 KB  |  101 lines

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: clamage@Eng.sun.com (Steve Clamage)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: "explicit" default constructor?
  5. Date: 28 Mar 1996 16:20:38 GMT
  6. Organization: Sun Microsystems Inc.
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4jeds0$h0b@engnews1.Eng.Sun.COM>
  9. References: <315A5C7A.6AF5@cs.tu-berlin.de>
  10. Reply-To: clamage@Eng.sun.com
  11. NNTP-Posting-Host: taumet.eng.sun.com
  12. X-Nntp-Posting-Host: taumet.eng.sun.com
  13. Content-Length: 2504
  14. X-Lines: 79
  15. Originator: clamage@taumet
  16.  
  17. In article 6AF5@cs.tu-berlin.de, Roman Lechtchinsky <wolfro@cs.tu-berlin.de> writes:
  18. >Steve Clamage wrote:
  19. >
  20. >> A converting constructor is one which can be called with a single parameter.
  21. >> The default constructor isn't a converting constructor except in the
  22. >> case of a constructor with all parameters having default values.
  23. >> 
  24. >> If a constructor has no parameters, declaring it "explicit" has no effect,
  25. >> since it can never be invoked for a conversion.
  26. >
  27. >> If a constructor's parameters all have default values, it can be a
  28. >> converting constructor, and declaring it "explicit" prevents it from
  29. >> being invoked implicitly.
  30. >
  31. >Yes, I've had this in mind, too. In the following case:
  32. >
  33. >class A
  34. >{
  35. > explicit A( int x=0 );
  36. >};
  37. >
  38. >how do you call the default constructor?
  39.  
  40. The same way you always did before. The default constructor is one which
  41. requires no parameters. If implicitly or explicitly invoked with no
  42. parameters, no conversion is taking place, and it is a non-converting
  43. constructor. 
  44.  
  45. > Can it still be invoked implicitly 
  46. >and what is the "explicit" syntax if not? IMO, something like
  47. >
  48. >A a;
  49. >
  50. >is not explicit 
  51.  
  52. I don't think it matters whether you consider this explicit or not.
  53. (I would say it is implicit.) No type conversion is taking place, so
  54. whether the constructor is declared "explicit" or not doesn't matter.
  55.  
  56. > An explicit call would rather be
  57. >
  58. >A a();
  59.  
  60. No, that declares 'a' to be a function with no parameters returning an
  61. 'A' by value. It is an unfortunate consequence of C declaration syntax
  62. colliding with C++ intialization syntax.
  63.  
  64. You can invoke the default constructor explicitly to create an anonymous
  65. temporary object in the same way as before, whether or not you supply
  66. parameters, and whether or not it is declared explicit. Examples:
  67.  
  68. class C {
  69. public:
  70.     C(int=0);
  71. };
  72.  
  73. foo( const C& );
  74.  
  75. foo( C() ); // #1
  76. foo( C(2) ); // #2
  77. foo( 3 );   // #3
  78.  
  79. In #1, C's (default) constructor is explicitly invoked to create a temp
  80. object to be passed to foo. It doesn't matter if the constructor was
  81. declared explicit or not in this case.
  82.  
  83. In #2, C's constructor is explicitly invoked to convert the int value 2 to
  84. a temp C object to be passed to foo. Again, C::C() can be explicit or not.
  85.  
  86. In #3, C's constructor is invoked implicitly to convert the int value 3 to
  87. a temp C object to be passed to foo. But if the constructor were declared
  88. "explicit", this use would be invalid. You would have to use the form of #2.
  89.  
  90. ---
  91. Steve Clamage, stephen.clamage@eng.sun.com
  92.  
  93.  
  94.  
  95.  
  96. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  97. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  98. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  99. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  100. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  101.